<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日期计算</title>
    <script>
        function riqicha(){
            let kaishi = document.querySelector("#kaishiriqi").value  //字符串类型 "2024-11-20"
             kaishi = new Date(kaishi); //日期类型
             let jieshu = document.querySelector("#jieshuriqi").value //字符串类型 "2024-12-31"
             jieshu = new Date(jieshu); //日期类型
             kaishi = kaishi.getTime(); //开始日期毫秒数，1970-1-1-0:0:0:0 - 2024-11-20-0:0:0:0中间的毫秒数 1000ms
             jieshu = jieshu.getTime(); //结束日期毫秒数，1970-1-1-0:0:0:0 - 2024-12-20-0:0:0:0中间的毫秒数 2000ms
             let cha = Math.round((jieshu - kaishi) / (24 * 3600 * 1000)) //相差天数
             document.querySelector("#chax").innerHTML =cha //显示相差天数
        }
    </script>
</head>
<body>
    <h1>计算日期差</h1>
    开始日期：<input type="date" id="kaishiriqi">
    <br>
    结束日期：<input type="date" id="jieshuriqi">
    <br>
    相差：<span id="chax">0</span>天
    <button onclick="riqicha()">计算日期差</button>
</body>
</html>